package com.medi.chats.controller.api.m;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medi.chats.consts.Consts;
import com.medi.chats.model.McChats;
import com.medi.chats.model.McUserBuy;
import com.medi.chats.model.McUsers;
import com.medi.chats.service.activities.ActivitiesService;
import com.medi.chats.service.category.CategoriesService;
import com.medi.chats.service.chats.ChatsService;
import com.medi.chats.service.chats.UserBuyService;
import com.medi.chats.utils.UtilsService;
import com.medi.chats.vo.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/m/api")
public class ChatsApiController {
    @Autowired
    ChatsService chatsService;
    @Autowired
    ActivitiesService activitiesService;
    @Autowired
    CategoriesService categoriesService;
    @Autowired
    UtilsService utilsService;
    @Autowired
    UserBuyService userBuyService;

    @PostMapping("/cat")
    private ResultMsg cat(@RequestBody Map<String, String> catParam) {
        ResultMsg rm = new ResultMsg();

        // 获取分类信息
        rm.add("catList", categoriesService.selectCategoriesWithAll());

        // 2.获取十条最新的chats
         PageHelper.startPage(utilsService.Object2Int(catParam.get("page")), utilsService.Object2Int(catParam.get("limit")));

        //查询条件
        Map<String, Object> map = new HashMap<>();
        map.put("chatCat", catParam.get("cat"));
        List<McChats> chatsList = chatsService.selectChatsByCat(map);
        rm.add("chatsList", chatsList);
        PageInfo<McChats> pageInfo = new PageInfo<>(chatsList);
        rm.add("hasNext", pageInfo.isHasNextPage());
        rm.setSuccess();
        return rm;
    }

    /**
     * @Description  获取文章详细内容
     * @Author        Administrator
     * @Date          2018/11/2 16:29
     */
    @PostMapping("/info/{id}")
    public ResultMsg info(@PathVariable("id") Long id, HttpServletRequest request) {
        ResultMsg rm = new ResultMsg();

        HttpSession session = request.getSession();
        // 1.用户是否登录
        if (session.getAttribute("chatUser") == null) {
            rm.add("isLogin", false);
            rm.add("isBuy", false);
        } else {
            rm.add("isLogin", true);

            McUsers mcUsers = (McUsers)session.getAttribute("chatUser");
            // 2.用户是否购买了当前的chat
            Map<String, Object> map = new HashMap<>();
            map.put("userId", mcUsers.getId());
            map.put("itemId", id);
            map.put("itemType", Consts.ITEM_TYPE_CHAT);
            McUserBuy userBuy = userBuyService.selectByUserItemType(map);
            if (userBuy == null) {
                rm.add("isBuy", false);
            } else {
                rm.add("isBuy", true);
            }
        }

        // 获取文章的详细信息
        McChats chatDetail = chatsService.getChatDetailById(id);
        rm.add("chatDetail", chatDetail);

        // 已预约用户的icon列表
        Map<String, Object> map = new HashMap<>();
        map.put("itemId", id);
        map.put("itemType", Consts.ITEM_TYPE_CHAT);

        // 只获取5个用户的头像
        PageHelper.startPage(1, 5);
        List<McUserBuy> userBuyList = userBuyService.selectBuyListByItemType(map);
        rm.add("buyList", userBuyList);

        // 当前用户是否关注了作者
        rm.add("focusStatus", false);

        rm.setSuccess();
        return rm;
    }

}
